package cn.aopmin.java.api.lang;

import cn.aopmin.java.api.util.diy.DateTimeUtils;

/**
 * 获取线程的状态信息
 * @author 白豆五
 * @since 2025/1/29
 */
public class ThreadStateTest {
    public static void main(String[] args) {
        PrintThread printThread = new PrintThread();
        printThread.setName("打印线程");
        // java.lang.Thread.State 线程状态枚举,线程状态有6种：NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED
        System.out.println(String.format("线程名称：%s ，线程状态：%s", printThread.getName(), printThread.getState()));
        printThread.start();
        System.out.println(String.format("线程名称：%s ，线程状态：%s", printThread.getName(), printThread.getState()));

        // 等待打印线程执行完成
        try {
            Thread.sleep(8000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(String.format("线程名称：%s ，线程状态：%s", printThread.getName(), printThread.getState()));
    }
}

class PrintThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);// 睡眠1秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            String str = String.format(" [%s]正在执行,当前时间：%s",Thread.currentThread().getName(), DateTimeUtils.getCurrentTime());
            System.out.println(str);
        }
    }
}